import approveApi from '@/api/src/approve'
import { get } from 'sortablejs'
export default {
  props: {
    // 入口是否为审批
    isApproval: {
      type: Boolean,
      default: false
    },
    draftId: {
      type: [String, Number],
      default: ''
    },
    isShowBtn: {
      // 是否展示按钮
      type: Boolean,
      default: true
    },
    isWaitMe: {
      type: String,
      default: '1'
    }
  },
  data () {
    return {
      initMethodsKey: 'getInfo',
      showApproveDialog: false,
      isLoadList: false // 加载审批记录
    }
  },
  computed: {
    approveObj: {
      get () {
        return {
          businessId: '180',
          processType: '1',
          remark: '',
          approveType: '0',
          draftId: this.draftId
        }
      },
      set () {
        console.log('approveObj set')
      }
    },
    isShowApproveBtn () {
      if (this.linkInfo.linkStatus === '3') {
        return false
      } else if (
        this.linkInfo.linkStatus === '2' &&
        this.linkInfo.isSponsor === '0'
      ) {
        return false
      } else if (['2', '4'].includes(this.isWaitMe)) {
        // 入口是“我收到的”和“我已审批” 不展示按钮
        return false
      } else {
        return true
      }
    },
    // 我发起的不展示“审批通过”和“审批驳回”按钮
    isHiddenApproveBtn () {
      if (this.isWaitMe === '3' && this.linkInfo.isSponsor === '1') {
        return false
      } else {
        return true
      }
    }
  },

  methods: {
    // 撤销审批
    onRevocation () {
      this.$msg
        .confirm('该审批流已开始，确定撤销审批？', '撤销审批')
        .then(async () => {
          await approveApi.cancel({ ...this.approveObj })
          this.$msg.success()
          this[this.initMethodsKey]()
          this.isLoadList = !this.isLoadList
          this.$emit('after-save')

          if (typeof this.approveNoMessage === 'function') {
            this.approveNoMessage()
          }
        })
    },

    async afterApprove (obj) {
      console.log(obj, this.processType, 'obj')
      this.approveObj.remark = obj.remark

      // 移入公海
      this.approveObj.businessId =
        this.processType === '11'
          ? this.businessId
          : this.approveObj.businessId
      this.approveObj.processType =
        this.processType === '11'
          ? this.processType
          : this.approveObj.processType

      let specialRes
      if (+obj.type === 1) {
        // type '1'通过 '0'驳回
        specialRes = await approveApi.agree({ ...this.approveObj })
      } else {
        specialRes = await approveApi.refuse({ ...this.approveObj })
      }
      if (['2', '8'].includes(this.approveObj.processType)) {
        this.specialID = specialRes.businessId
        this.specialDraftID = specialRes.draftId
      }
      this.$refs.approveDialog.dialogClose()
      this.$msg.success()
      this[this.initMethodsKey]()
      this.isLoadList = !this.isLoadList
      this.$emit('after-save')

      if (typeof this.approveNoMessage === 'function') {
        this.approveNoMessage()
      }
    },
    // 审批驳回或通过
    onApproved (type, obj) {
      if (type === '1' && obj && obj.type === '4' && !obj.minSalePrice) {
        this.$message.error(
          '包场销售单的最低销售单价必填,请进入编辑页编辑。'
        )
        return
      }
      this.approveObj.approveType = type
      this.showApproveDialog = true
    },
    async onSubmit () {
      const row = this.infoData
      if (+row.isComplete === 1) {
        await approveApi.submit({
          processType: this.processType,
          businessId: row.id,
          linkName: row.name
        })
        this[this.initMethodsKey]()
        this.isLoadList = !this.isLoadList
        this.$emit('after-save')
        this.$message.success('操作成功')

        if (typeof this.approveNoMessage === 'function') {
          this.approveNoMessage()
        }
      } else {
        this.$message.warning('信息未填写完整，请重新编辑后再提交审批')
      }
    },
    // 系统通知详情回调
    closeAfter () {
      this.close()
      this.$emit('load-list')
    }
  }
}
